package com.wholesale;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.wholesale.beans.Feedback;
import com.wholesale.datas.WebServiceClient;
import com.wholesale.utils.Constants;

public class MyFeedBackLayout extends LinearLayout implements OnClickListener {
	private ViewFlipper viewFlipper;
	private Context context;
	private View baseView;
	private ListView bodyList;
	private Button leftButton;
	private Button rightButton;
	private TextView headView;
	private RelativeLayout loadingLayout;

	public MyFeedBackLayout(Context context, final ViewFlipper viewFlipper) {
		super(context);
		this.viewFlipper = viewFlipper;
		this.context = context;
		baseView = ((Activity) getContext()).getLayoutInflater().inflate(
				R.layout.common, this);
		bodyList = (ListView) baseView.findViewById(R.id.bodyList);
		leftButton = (Button) baseView.findViewById(R.id.leftBtn);
		leftButton.setText("Back");
		leftButton.setOnClickListener(this);
		rightButton = (Button) baseView.findViewById(R.id.rightBtn);
		rightButton.setVisibility(View.INVISIBLE);
		headView = (TextView) baseView.findViewById(R.id.title);
		headView.setText("My Feedback");
		loadingLayout = (RelativeLayout) baseView
		.findViewById(R.id.loadingLayout);
		
		bodyList.setSelector(R.drawable.comments_list_bg);
		FeedBackAsync feedBackAsync = new FeedBackAsync(context);
		feedBackAsync.execute((Void)null);
		

	}

	OnItemClickListener moreClieckListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {

		}
	};

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftBtn:
			viewFlipper.showPrevious();
			viewFlipper.removeView(this);
			break;
		default:
			break;
		}
	}
	

	class FeedBackAsync extends
			AsyncTask<Void, Integer, ArrayList<Feedback>> {
		ProgressDialog pdialog;
		public FeedBackAsync(Context context) {
//			pdialog = new ProgressDialog(context);
//			pdialog.setTitle("Loading...");
//			pdialog.setMessage("Please wait...");
//			pdialog.setCancelable(true);
//			pdialog.setButton("cancel", new DialogInterface.OnClickListener() {
//				public void onClick(DialogInterface dialog, int i) {
//					dialog.cancel();
//					FeedBackAsync.this.cancel(true);
//				}
//			});
//			pdialog.show();
		}

		@Override
		protected void onPreExecute() {
			loadingLayout.setVisibility(View.VISIBLE);
		}
		
		@Override
		protected ArrayList<Feedback> doInBackground(Void... params) {
				return WebServiceClient.getFeedback(Constants.loginResponse.getUserId(), Constants.loginResponse.getUserToken());
		}

		@Override
		protected void onPostExecute(ArrayList<Feedback> result) {
			List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
			for(Feedback feedback : result){
				HashMap<String, Object> map1 = new HashMap<String, Object>();
				map1.put("title", feedback.getMsgTitle());
				map1.put("comment", feedback.getMsgContent());
				map1.put("from", feedback.getUserName());
				if(feedback.getMsgTime() != null){
					map1.put("date", feedback.getMsgTime().toString());
				}
				data.add(map1);
			}
			SimpleAdapter listItemAdapter = new SimpleAdapter(
	        		context, data,// 数据源
					R.layout.feedbackitem,// ListItem的XML实现
					new String[] { "title","date", "comment","from" }, new int[] {
							R.id.title_text,
							R.id.senddate_text,
							R.id.comments_text,
							R.id.from_text
							});
	        bodyList.setAdapter(listItemAdapter);
//			pdialog.dismiss();
	        loadingLayout.setVisibility(View.GONE);
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}
	}
}
